package com.graduation.controller;

import com.graduation.config.ObjectConfig;
import com.graduation.entity.Goods;
import com.graduation.entity.Order;
import com.graduation.entity.Recommend;
import com.graduation.entity.User;
import com.graduation.service.GoodsService;
import com.graduation.service.OrderService;
import com.graduation.service.UserService;
import com.graduation.util.DateUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;

@Controller
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private UserService userService;

    /**
     * 创建订单
     *
     * @param request
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/order.do", method = RequestMethod.GET)
    public ModelAndView order(HttpServletRequest request, @RequestParam(name = "goodsId") Integer... goodsId) {
        //拿到商品集合
        List<Recommend> list = orderService.doOrder(goodsId);
        Double price = 0.0;
        ApplicationContext ac = new AnnotationConfigApplicationContext(ObjectConfig.class);
        ModelAndView mv = (ModelAndView) ac.getBean("createMandV");
        mv.addObject("list", list);
        mv.addObject("totalNum", list.size());
        for (Recommend re : list) {
            price += Double.valueOf(re.getPrice());
        }
        //当前登录的用户数据
        User user = (User) request.getSession(false).getAttribute("user");
        User u = userService.findUserAddrAndName(user.getEmail());
        String realName = u.getFirstname() + u.getLastname();
        String address = u.getAddress();
        if ("".equals(address) || address == null) {
            u.setAddress("尚未完善地址,请前往个人主页完善信息！");
        }
        mv.addObject("realName", realName);
        mv.addObject("addr", u.getAddress());
        mv.addObject("tel", u.getTel());
        mv.addObject("totalPrice", price);
        mv.setViewName("order");
        return mv;
    }

    /**
     * 提交订单
     *
     * @param request
     * @param totalPrice
     * @return
     */
    @RequestMapping("/submitOrder.do")
    public ModelAndView submitOrder(HttpServletRequest request, String totalPrice, String remarks) {
        //存放数组
        List<Goods> list = new ArrayList<>();
        //拿到容器中的ModelAndView
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ObjectConfig.class);
        ModelAndView mv = (ModelAndView) ac.getBean("createMandV");
        String[] fileNames = request.getParameterValues("fileName");
        String[] goodsName = request.getParameterValues("goodsName");
        String[] price = request.getParameterValues("price");
        String[] stock = request.getParameterValues("stock");
        User user = (User) request.getSession(false).getAttribute("user");
        for (int i = 0; i < fileNames.length; i++) {
            Goods goods = new Goods();
            goods.setFileName(fileNames[i]);
            goods.setGoodsName(goodsName[i]);
            goods.setPrice(Double.parseDouble(price[i]));
            goods.setStock(Integer.valueOf(stock[i]));
            goods.setRemarks(remarks);
            goods.setEmail(user.getEmail());
            list.add(goods);
        }
        Integer integer = orderService.doSaveOrder(list);
        //清空购物车(尚未实现)
        mv.setViewName("order-success");
        mv.addObject("orderNum", System.currentTimeMillis());
        mv.addObject("totalPrice", totalPrice);
        return mv;
    }

    /**
     * 查询订单
     * @param request
     * @return
     */
    @PostMapping("/findOrders.do")
    @ResponseBody
    public List<Goods> findGoodsOrder(HttpServletRequest request){
        User user = (User) request.getSession(false).getAttribute("user");
        return orderService.findGoodsByEmail(user.getEmail());
    }
    /*计算总价*/
    @PostMapping("/findTotalPrice.do")
    @ResponseBody
    public String findTotalPrice(HttpServletRequest request){
        double price = 0;
        User user = (User) request.getSession(false).getAttribute("user");
        List<Order> goodsByEmail = orderService.findGoodsByEmail(user.getEmail());
        for (Order g : goodsByEmail){
            price += g.getPrice();
        }
        return String.valueOf(price);
    }
}
